<?php

require_once(WSB_MODULES_PATH . 'sherpa/forms/SherpaForm.php');

class Sherpa_Form_SherpaForm extends WSB_Xtension_Form
{

	public $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array('Description', array('tag' => 'p', 'class' => 'description')),
        array(array('data' => 'HtmlTag'), array('tag' => 'dd')),
        array('Label', array('tag' => 'dt', 'requiredSuffix' => '<span class="required">*</span>', 'escape' => false))
    );

	public $hiddenDecorators = array(
        'ViewHelper',
        array(array('data' => 'HtmlTag'), array('tag' => 'dd', 'class' => 'element', 'style' => 'display:none')),
        array(array('label' => 'HtmlTag'), array('tag' => 'dt', 'style' => 'display:none', 'placement' => 'prepend'))
    );

    public $buttonDecorators = array(
        'ViewHelper',
        array('Description', array('tag' => 'p', 'class' => 'description')),
        array(array('data' => 'HtmlTag'), array('tag' => 'dd', 'class' => 'button')),
        array(array('label' => 'HtmlTag'), array('tag' => 'dt', 'style' => 'display:none', 'placement' => 'prepend'))
    );

	/**
	 * Uebersteuert WSB_Xtension_Form::useDefaultDecorators()
	 */
	public function useDefaultDecorators()
	{
        foreach($this->getElements() AS $element) {
			switch(get_class($element)) {
				case 'Zend_Form_Element_Hidden':
					$element->setDecorators($this->hiddenDecorators);
					break;
				case 'Zend_Form_Element_Submit':
				case 'Zend_Form_Element_Image':
					$element->setDecorators($this->buttonDecorators);
					break;
				default:
					$element->setDecorators($this->elementDecorators);
					break;
			}
        }

        foreach($this->getElements() AS $element) {
			switch(get_class($element)) {
				case 'Zend_Form_Element_Text':
				case 'Zend_Form_Element_Password':
					$element->setAttrib('class', 'textfield');
					break;
				default:
					break;
			}
        }

        $formName = $this->getName();

        $displayGroups = $this->getDisplayGroups();
        if(empty($displayGroups)) {
	        $this->clearDecorators();
	        $this->addDecorator('FormElements')
	             ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => empty($formName) ? 'default-form' : $formName ))
	             ->addDecorator('Form');
        } else {
	        $this->clearDecorators();
		    $this->addDecorator('FormElements')
		         //->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => empty($formName) ? 'default-form' : 'default-form ' . $formName ))
		         ->addDecorator('Form');
        }

        $this->setDefaultDisplayGroupDecorators();

        // Erforderliche Angaben
		$element = new Zend_Form_Element_Text('required');
		$element->getDecorator('ViewHelper')->setOption('helper', 'FormNote');
		$element->setValue('<span class="required">*</span> erforderliche Angaben');
		$this->addElement($element);

		// Buttons zuletzt ausgeben
		$i = 1000;
        foreach($this->getElements() AS $element) {
			switch(get_class($element)) {
				case 'Zend_Form_Element_Submit':
				case 'Zend_Form_Element_Image':
					$element->setOrder($i);
					$i++;
					break;
			}
        }

	}

}
